package com.lframework.xingyun.basedata.impl.member;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.core.components.resp.PageResult;
import com.lframework.starter.web.core.utils.OpLogUtil;
import java.io.Serializable;
import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import com.lframework.starter.web.core.utils.IdUtil;
import com.lframework.starter.common.utils.ObjectUtil;
import com.lframework.starter.web.core.annotations.oplog.OpLog;
import com.lframework.starter.common.utils.Assert;
import com.lframework.starter.web.inner.components.oplog.OtherOpLogType;
import com.lframework.xingyun.basedata.entity.MemberLevel;
import com.lframework.xingyun.basedata.mappers.MemberLevelMapper;
import com.lframework.xingyun.basedata.service.member.MemberLevelService;
import com.lframework.xingyun.basedata.vo.member.CreateMemberLevelVo;
import com.lframework.xingyun.basedata.vo.member.QueryMemberLevelSelectorVo;
import com.lframework.xingyun.basedata.vo.member.QueryMemberLevelVo;
import com.lframework.xingyun.basedata.vo.member.UpdateMemberLevelVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MemberLevelServiceImpl extends BaseMpServiceImpl<MemberLevelMapper, MemberLevel> implements MemberLevelService {

    @Override
    public PageResult<MemberLevel> query(Integer pageIndex, Integer pageSize, QueryMemberLevelVo vo) {

        Assert.greaterThanZero(pageIndex);
        Assert.greaterThanZero(pageSize);

        vo.setPageIndex(pageIndex);
        vo.setPageSize(pageSize);

        IPage<MemberLevel> page = getBaseMapper().query(vo);

        return PageResultUtil.convert(page);
    }

    @Override
    public List<MemberLevel> query(QueryMemberLevelVo vo) {

        return getBaseMapper().queryList(vo);
    }

    @Override
    public MemberLevel queryForever() {
        return getBaseMapper().queryForever();
    }

    @Cacheable(value = MemberLevel.CACHE_NAME, key = "@cacheVariables.tenantId() + #id", unless = "#result == null")
    @Override
    public MemberLevel findById(String id) {

        return getBaseMapper().selectById(id);
    }

    @OpLog(type = OtherOpLogType.class, name = "新增会员等级配置，ID：{}", params = {"#id"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String create(CreateMemberLevelVo vo) {

        MemberLevel data = new MemberLevel();
        data.setId(IdUtil.getId());
        data.setLevel(vo.getLevel());
        data.setLevelName(vo.getLevelName());
        data.setUpgradeType(vo.getUpgradeType());
        data.setUpgradeCondition(vo.getUpgradeCondition());
        data.setUpgradeDesc(vo.getUpgradeDesc());
        data.setEffectiveDay(vo.getEffectiveDay());
        data.setAvailable(vo.getAvailable());

        getBaseMapper().insert(data);

        OpLogUtil.setVariable("id", data.getId());
        OpLogUtil.setExtra(vo);

        return data.getId();
    }

    @OpLog(type = OtherOpLogType.class, name = "修改会员等级配置，ID：{}", params = {"#id"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(UpdateMemberLevelVo vo) {

        MemberLevel data = getBaseMapper().selectById(vo.getId());
        if (ObjectUtil.isNull(data)) {
            throw new DefaultClientException("会员等级配置不存在！");
        }

        LambdaUpdateWrapper<MemberLevel> updateWrapper = Wrappers.lambdaUpdate(MemberLevel.class)
                .set(MemberLevel::getLevel, vo.getLevel())
                .set(MemberLevel::getLevelName, vo.getLevelName())
                .set(MemberLevel::getUpgradeType, vo.getUpgradeType())
                .set(MemberLevel::getUpgradeCondition, vo.getUpgradeCondition())
                .set(MemberLevel::getUpgradeDesc, vo.getUpgradeDesc())
                .set(MemberLevel::getEffectiveDay, vo.getEffectiveDay())
                .set(MemberLevel::getAvailable, vo.getAvailable())
                .eq(MemberLevel::getId, vo.getId());

        getBaseMapper().update(updateWrapper);

        OpLogUtil.setVariable("id", data.getId());
        OpLogUtil.setExtra(vo);
    }

    @Override
    public PageResult<MemberLevel> selector(Integer pageIndex, Integer pageSize, QueryMemberLevelSelectorVo vo) {
        Assert.greaterThanZero(pageIndex);
        Assert.greaterThanZero(pageSize);

        vo.setPageIndex(pageIndex);
        vo.setPageSize(pageSize);

        IPage<MemberLevel> selector = getBaseMapper().selector(vo);

        return PageResultUtil.convert(selector);
    }

    @CacheEvict(value = MemberLevel.CACHE_NAME, key = "@cacheVariables.tenantId() + #key")
    @Override
    public void cleanCacheByKey(Serializable key) {

    }
}

